import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/health_reminder_provider.dart';

class HealthReminderPage extends StatefulWidget {
  const HealthReminderPage({super.key});

  @override
  State<HealthReminderPage> createState() => _HealthReminderPageState();
}

class _HealthReminderPageState extends State<HealthReminderPage> {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      Provider.of<HealthReminderProvider>(context, listen: false)
          .fetchHealthReminder();
    });
  }

  @override
  Widget build(BuildContext context) {
    final provider = Provider.of<HealthReminderProvider>(context);

    return SingleChildScrollView(
      child: Center(
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            mainAxisSize: MainAxisSize.min,
            children: [
              const Icon(
                Icons.tips_and_updates,
                size: 60,
                color: Colors.green,
              ),
              const SizedBox(height: 16),
              if (provider.isLoading)
                const CircularProgressIndicator()
              else if (provider.errorMessage.isNotEmpty)
                Text(
                  provider.errorMessage,
                  style: const TextStyle(color: Colors.red),
                )
              else
                SingleChildScrollView(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Flexible(
                        child: Text(
                          provider.tipContent,
                          style: const TextStyle(
                            fontSize: 18,
                            fontWeight: FontWeight.bold,
                          ),
                          textAlign: TextAlign.center,
                        ),
                      ),
                    ],
                  ),
                ),
              const SizedBox(height: 16),
              ElevatedButton.icon(
                onPressed: () => provider.fetchHealthReminder(),
                icon: const Icon(Icons.refresh),
                label: const Text('换一条',
                    style: TextStyle(fontWeight: FontWeight.bold)),
                style: ElevatedButton.styleFrom(
                  backgroundColor: Theme.of(context).colorScheme.primary,
                  foregroundColor: Colors.white,
                  padding: const EdgeInsets.symmetric(
                    horizontal: 24,
                    vertical: 12,
                  ),
                  elevation: 2,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(20),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
